package tech.lq0.dreamaticvoyage.voyage.core;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import tech.lq0.dreamaticvoyage.Utils;

import java.util.ArrayList;
import java.util.List;

public class VoyageEvent {

    // 注册名
    public final String descriptionId;
    // 类型
    public final Type type;
    // 事件出现的属性要求
    public final float[] appearCondition;
    // 事件结果
    public final VoyageResult[] results;
    // 是否为隐藏事件
    public final boolean hidden;
    // 事件的等级
    public final int level;
    // 判断出的事件结果
    private VoyageResult selectedResult = VoyageResult.EMPTY;

    public VoyageEvent(Builder builder) {
        this.descriptionId = builder.descriptionId;
        this.type = builder.type;
        this.appearCondition = builder.appearCondition;
        this.results = builder.results.toArray(new VoyageResult[0]);
        this.hidden = builder.hidden;
        this.level = builder.level;
    }

    public VoyageResult getSelectedResult() {
        return selectedResult;
    }

    public void setSelectedResult(VoyageResult selectedResult) {
        this.selectedResult = selectedResult;
    }

    public enum Type {
        BENEFICIAL,
        NEUTRAL,
        HARMFUL,
    }

    public MutableComponent getDisplayName() {
        return Component.translatable("voyage." + Utils.MOD_ID + "." + this.descriptionId + ".name");
    }

    public MutableComponent getDescription() {
        return Component.translatable("voyage." + Utils.MOD_ID + "." + this.descriptionId + ".des");
    }

    public MutableComponent getSuccessMessage() {
        return Component.translatable("voyage." + Utils.MOD_ID + "." + this.descriptionId + ".des.success");
    }

    public MutableComponent getFailMessage() {
        return Component.translatable("voyage." + Utils.MOD_ID + "." + this.descriptionId + ".des.fail");
    }

    public static class Builder {

        String descriptionId;
        Type type;
        float[] appearCondition;
        List<VoyageResult> results = new ArrayList<>();
        boolean hidden;
        int level;

        public Builder id(String descriptionId) {
            this.descriptionId = descriptionId;
            return this;
        }

        public Builder type(Type type) {
            this.type = type;
            return this;
        }

        public Builder appearCondition(float[] appearCondition) {
            this.appearCondition = appearCondition;
            return this;
        }

        public Builder addResult(VoyageResult result) {
            this.results.add(result);
            return this;
        }

        public Builder hidden(boolean hidden) {
            this.hidden = hidden;
            return this;
        }

        public Builder level(int level) {
            this.level = level;
            return this;
        }
    }

    // TODO 完成出现条件的解析
    public static VoyageEvent createFromJson(ResourceLocation location, JsonObject jsonObject) {
        VoyageEvent.Builder builder = new VoyageEvent.Builder();
        builder.id(location.toString());
        builder.level(GsonHelper.getAsInt(jsonObject, "level", 1));
        Type type;
        try {
            type = Type.valueOf(GsonHelper.getAsString(jsonObject, "type", "NEUTRAL"));
        } catch (IllegalArgumentException e) {
            type = Type.BENEFICIAL;
        }
        builder.type(type);
        JsonElement results = GsonHelper.isArrayNode(jsonObject, "results") ?
                GsonHelper.getAsJsonArray(jsonObject, "results") : GsonHelper.getAsJsonObject(jsonObject, "results");
        var resultList = VoyageResult.createFromJson(results);
        for (var result : resultList) {
            builder.addResult(result);
        }
        builder.hidden(GsonHelper.getAsBoolean(jsonObject, "hidden", false));

//        builder.appearCondition(new float[]{0, 0, 0, 0});

        return new VoyageEvent(builder);
    }
}
